package com.tms.ned.controlmonitor;

import java.util.EventObject;

import com.tms.ned.controlmonitor.adapters.ComponentAdapter;

/*
 * Licensed under the Apache License, Version 2.0;<br> 
 * 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless 
 * required by applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and limitations under the License. 
 */


/**
 * Event signifying that a component has changed. The ControlMonitor framework
 * boils any component's changes into a single ComponentChangeEvent.
 *
 * @author bshannon
 *
 */
public class ComponentChangeEvent extends EventObject {

	private ComponentAdapter adapter;

	/**
	 * Constructs a ComponentChangeEvent using the source and the
	 * ComponentAdapter that caused the event.
	 *
	 * @param source
	 *            The source of the event.
	 * @param adapter
	 *            The ComponentAdapter adapting the component that changed.
	 */
	public ComponentChangeEvent(Object source, ComponentAdapter adapter) {
		super(source);
		this.adapter = adapter;
	}

	/**
	 *
	 * @return the component adapter that is adapting the changed component's
	 *         events.
	 */
	public final ComponentAdapter getComponentAdapter() {
		return adapter;
	}

	/**
	 *
	 * @return True if the component is to be considered "dirty" (that is, its
	 *         original value doesn't match its current value.
	 */
	public final boolean isDirty() {
		return adapter.isDirty();
	}


	/**
	 * Shortcut to check the validity of the component adapter that caused this event.
	 * @return TRUE if the component adapter is considered to be in the VALID state.
	 */
	public final boolean isValid() {
		return adapter.isValid();
	}

	@Override
	public final String toString() {
		return "ComponentChangeEvent\nName: '" + adapter.getName() + "'" +
		"\nDirty: " + isDirty() +
		"\nValid: " + adapter.getCurrentValidationStatus().isValid() +
		"\nValidationStatus: " + adapter.getCurrentValidationStatus() +
		"\nInitial Value:    [" + adapter.getInitialValue() + "]" +
		"\nCurrent Value:    [" + adapter.getCurrentValue() + "]";
	}

}
